package com.wuxiaofan.admin.config;

import com.alibaba.fastjson.JSONObject;
import com.wuxiaofan.admin.client.MemberClient;
import com.wuxiaofan.common.vo.ReturnResult;
import java.util.Arrays;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author wuxiaofan
 * @date 2021/11/9 10:07 上午
 */
@Slf4j
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Resource
    private MemberClient memberClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ReturnResult returnResult = memberClient.findByUsername(username);
        log.info("{}登录信息:{}",username,ObjectUtils.toString(returnResult.getResult()));
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(returnResult.getResult()));
        String password = jsonObject.getString("password");
        return new User(jsonObject.getString("username"),password,
                Arrays.asList(new SimpleGrantedAuthority("admin")));
    }

}
